package com.example.administrator.wallet.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.model.bean.WebSocketTransactionsBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/8/29 0029.
 */

public class TransactionDetailsFragmentAdapter extends BaseAdapter {

    private Context context;
    private List<WebSocketTransactionsBean.ResultBean> list;
    private TransactionDetailsFragmentAdapterViewHolder holder;

    public TransactionDetailsFragmentAdapter(Context context, List<WebSocketTransactionsBean.ResultBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list !=null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            holder = new TransactionDetailsFragmentAdapterViewHolder();
            convertView = View.inflate(context, R.layout.item_transaction_details_fragment_list,null);

            holder.tvtime = (TextView) convertView.findViewById(R.id.tv_time_transaction_details_fragment_list);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price_transaction_details_fragment_list);
            holder.tvQuantity = (TextView) convertView.findViewById(R.id.tv_quantity_transaction_details_fragment_list);

            convertView.setTag(holder);
        } else {
            holder = (TransactionDetailsFragmentAdapterViewHolder) convertView.getTag();
        }

        long time = (long) list.get(position).getTime() * 1000;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date(time);
        holder.tvtime.setText(simpleDateFormat.format(date));

        holder.tvPrice.setText(list.get(position).getPrice());

        if (list.get(position).getType().equals("sell")){
            holder.tvPrice.setTextColor(context.getResources().getColor(R.color.sell_color));
        } else {
            holder.tvPrice.setTextColor(context.getResources().getColor(R.color.buy_color));
        }

        holder.tvQuantity.setText(list.get(position).getAmount());

        return convertView;
    }

    public static class TransactionDetailsFragmentAdapterViewHolder{
        TextView tvtime,tvPrice,tvQuantity;
    }
}
